package com.dxq.test.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.dxq.test.App;
import com.dxq.test.common.AppManager;
import com.dxq.test.R;
import com.dxq.test.db.Event;
import com.dxq.test.db.Label;
import com.dxq.test.db.LabelEvent;
import com.dxq.test.db.Pasture;
import com.dxq.test.db.TablesDao;
import com.dxq.test.utils.DialogClickListener;
import com.dxq.test.utils.MyUtil;

import java.util.List;

/**
 * Author: zsh
 * Date: 2023/11/16
 * Describe: 事件添加、修改、删除页面
 */
public class EventAddActivity extends AppCompatActivity implements View.OnClickListener {

    public TablesDao tablesDao;

    private ImageView ivBack;
    private TextView tvTitle, tvSet;
    private Button btnSubmit;
    private EditText etName, etInfo;

    private Event event;
    private Label label;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //添加Activity到堆栈
        AppManager.getAppManager().addActivity(this);
        setContentView(R.layout.activity_event_add);
        event = (Event) getIntent().getSerializableExtra("event");
        label = (Label) getIntent().getSerializableExtra("label");
        tablesDao = new TablesDao(this);
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在appManager结束当前activity
        AppManager.getAppManager().finishActivity(this);
    }

    private void initView() {
        ivBack = findViewById(R.id.ivBack);
        tvTitle = findViewById(R.id.tvTitle);
        tvSet = findViewById(R.id.tvSet);
        etName = findViewById(R.id.etName);
        etInfo = findViewById(R.id.etInfo);
        btnSubmit = findViewById(R.id.btnSubmit);
        if (null == event) {
            tvTitle.setText("添加事件");
            btnSubmit.setText("新增");
            tvSet.setVisibility(View.GONE);
        } else {
            tvTitle.setText("修改事件");
            btnSubmit.setText("修改");
            tvSet.setVisibility(View.VISIBLE);
            tvSet.setText("删除");
            etName.setText(event.getName());
            etInfo.setText(event.getInfo());
        }

        ivBack.setOnClickListener(this);
        tvSet.setOnClickListener(this);
        btnSubmit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack:
                this.finish();
                break;

            case R.id.tvSet://删除
                MyUtil.normalDialog(EventAddActivity.this, "操作提示", "是否删除当前事件？",
                        "取消", "确定", new DialogClickListener() {

                            @Override
                            public void onNegativeClick() {

                            }

                            @Override
                            public void onPositiveClick() {
                                deleteEvent();
                            }
                        });
                break;

            case R.id.btnSubmit://提交
                submit();
                break;
        }
    }

    //删除event
    private void deleteEvent() {
        int result = tablesDao.deleteEvent(event);
        if (result != 1) {
            ToastUtils.showShort("删除失败, event");
            return;
        }
        //接着删除关联表
        List<LabelEvent> labelEvents = tablesDao.queryLabelEventByEvent(event);
        int res = tablesDao.deleteLabelEvent(labelEvents);
        ToastUtils.showShort("删除成功");
        this.finish();
    }

    private void submit() {
        //查询当前所选择牧场
        Pasture pasture = tablesDao.queryPastureById(App.select_pasture_id);
        String name = etName.getText().toString();
        String info = etInfo.getText().toString();
        if (name.equals("")) {
            ToastUtils.showShort("输入标题");
            return;
        }
        if (info.equals("")) {
            ToastUtils.showShort("输入内容");
            return;
        }
        if (null == event) {
            //新增
            Event event = new Event(name, info, TimeUtils.getNowString(), pasture);
            //1、创建事件
            int result = tablesDao.insertEvent(event);
            if (result == 1) {
                //2、创建牲畜事件关联数据
                LabelEvent labelEvent = new LabelEvent(label, event);
                int res = tablesDao.insertLabelEvent(labelEvent);
                if (res == 1) {
                    //3、创建成功，返回页面
                    ToastUtils.showShort("新增成功");
                    this.finish();
                } else {
                    //4、创建失败，删除创建成功的事件数据
                    ToastUtils.showShort("新增失败，牲畜事件");
                    tablesDao.deleteEvent(event);
                }
                return;
            }
            ToastUtils.showShort("新增失败，事件");
        } else {
            //修改
            Event e = tablesDao.queryEventById(event.getEvent_id());
            e.setName(name);
            e.setInfo(info);
            int result = tablesDao.updateEvent(e);
            if (result == 1) {
                ToastUtils.showShort("更新成功");
                this.finish();
                return;
            }
            ToastUtils.showShort("更新失败");
        }
    }

    @Override
    public void onBackPressed() {
        this.finish();
    }
}
